package com.ostrich.note.ui.note.views;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmFragment;
import com.ostrich.note.databinding.FragmentNoteBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.event.Event;
import com.ostrich.note.ui.note.adapter.NoteAdapter;
import com.ostrich.note.ui.note.adapter.NoteTypeAdapter;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.ui.note.entity.TypeEntity;
import com.ostrich.note.ui.note.viewmodel.NoteViewModel;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashSet;
import java.util.List;


public class NoteFragment extends BaseMvvmFragment<FragmentNoteBinding, NoteViewModel>{
    private NoteAdapter mNoteAdapter;
    private NoteTypeAdapter mNoteTypeAdapter;
    private TypeEntity data;

    @Override
    public int getLayout() {
        return R.layout.fragment_note;
    }

    @Override
    public void initData() {
        //笔记列表
        viewModel.notepadDataList(0).observe(this, new Observer<BaseResponseEntity<List<NoteEntity>>>() {
            @Override
            public void onChanged(BaseResponseEntity<List<NoteEntity>> listBaseResponseEntity) {
                List<NoteEntity> data = listBaseResponseEntity.getData();
                mNoteAdapter.setData(data);//刷新数据
            }
        });
        //分类列表数据
        viewModel.notepadClassifiedData().observe(NoteFragment.this, new Observer<BaseResponseEntity<TypeEntity>>() {
            @Override
            public void onChanged(BaseResponseEntity<TypeEntity> typeEntityBaseResponseEntity) {
                data = typeEntityBaseResponseEntity.getData();


            }
        });

    }

    @Override
    public void initView() {
        mNoteAdapter = new NoteAdapter();
        binding.rv.setAdapter(mNoteAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rv.setHasFixedSize(true);
    }

    @Override
    public void initListener() {
        mNoteAdapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("noteData",mNoteAdapter.getList());
                bundle.putInt("position",position);
                startActivityBundle(NoteDetailActivity.class,bundle);
            }
        });
        //点击全部弹出窗体
        binding.clAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(v);

            }
        });
        //点击搜索
        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(NoteSearchActivity.class);
            }
        });
        //点击写笔记
        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(NoteAddActivity.class);
            }
        });


        //上拉下拉功能
        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                //笔记列表
                viewModel.notepadDataList(0).observe(NoteFragment.this, new Observer<BaseResponseEntity<List<NoteEntity>>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<List<NoteEntity>> listBaseResponseEntity) {
                        List<NoteEntity> data = listBaseResponseEntity.getData();
                        mNoteAdapter.setData(data);//刷新数据
                    }
                });
                binding.refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                binding.refreshLayout.finishRefresh();
            }
        });


    }


    private void showPopupWindow(View v) {
        //数据预加载成功后才弹出窗体，不然报错
        if(data == null){
            return;
        }
        PopupWindow popupWindow = new PopupWindow(getActivity());
        popupWindow.setBackgroundDrawable(null);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_note_type, null);
        RecyclerView recyclerView = view.findViewById(R.id.rv);
        mNoteTypeAdapter = new NoteTypeAdapter();
        recyclerView.setAdapter(mNoteTypeAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mNoteTypeAdapter.setData(data.getList());

        view.findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",data);
                startActivityBundle(TypeEditActivity.class,bundle);

            }
        });
        popupWindow.setContentView(view);
        popupWindow.setWidth(400);//不规范 px->dp
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(v,-170,0, Gravity.BOTTOM|Gravity.CENTER);//不规范

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(getActivity()).get(NoteViewModel.class);
    }

    @Override
    public void onEvent(Event event) {
        super.onEvent(event);
        if(event.type == Event.NOTE_ADD){

            viewModel.notepadDataList(0).observe(this, new Observer<BaseResponseEntity<List<NoteEntity>>>() {
                @Override
                public void onChanged(BaseResponseEntity<List<NoteEntity>> listBaseResponseEntity) {
                    List<NoteEntity> data = listBaseResponseEntity.getData();
                    NoteEntity note = data.get(data.size() - 1);
                    mNoteAdapter.addData(note);
                    binding.rv.getItemAnimator().setAddDuration(0);
                }
            });

        }else if(event.type == Event.NOTE_DELETE){
           int position = (int) event.data;
            mNoteAdapter.deleteData(position);
            binding.rv.getItemAnimator().setRemoveDuration(0);
        }else if(event.type == Event.NOTE_UPDATE){
            int position = (int) event.data;
            String title  = (String) event.data2;
            String content  = (String) event.data3;
            mNoteAdapter.updateData(title,content,position);
            binding.rv.getItemAnimator().setChangeDuration(0);
        }







    }
}